package com.tl.boot.service.resource.impl;

import com.tl.boot.common.enums.StatusEnum;
import com.tl.boot.common.utils.DateUtil;
import com.tl.boot.entity.permission.domain.ResourceInfo;
import com.tl.boot.entity.permission.dto.AddResourceInfoDTO;
import com.tl.boot.entity.permission.dto.ListResourceInfoDTO;
import com.tl.boot.entity.permission.dto.UpdateResourceInfoDTO;
import com.tl.boot.entity.permission.vo.ListResourceInfoVO;
import com.tl.boot.entity.permission.vo.QueryResourceInfoVO;
import com.tl.boot.mapper.resource.ResourceMapper;
import com.tl.boot.service.resource.ResourceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * com.tl.boot.service.resource.impl -- ResourceServiceImpl
 * Description:
 * Created by joshua_liu <a href="mailto:joshualwork@163.com"></a>
 * On 2019/9/19 10:18
 */
@Service
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    private ResourceMapper mapper;
    @Override
    public List<ListResourceInfoVO> listResourceInfo(ListResourceInfoDTO dto) {
        ResourceInfo resourceInfo = new ResourceInfo();
        BeanUtils.copyProperties(dto,resourceInfo);
        return mapper.listResourceInfo(resourceInfo);
    }

    @Override
    public Long addResourceInfo(AddResourceInfoDTO dto) {
        ResourceInfo resourceInfo = new ResourceInfo();
        BeanUtils.copyProperties(dto,resourceInfo);
        mapper.insertResourceInfo(resourceInfo);
        return resourceInfo.getResId();
    }

    @Override
    public void delResourceInfo(Long resId) {
        mapper.delResourceInfo(resId);
    }

    @Override
    public void updateResourceInfo(UpdateResourceInfoDTO dto) {
        ResourceInfo resourceInfo = new ResourceInfo();
        BeanUtils.copyProperties(dto,resourceInfo);
        mapper.updateResourceInfo(resourceInfo);
    }

    @Override
    public QueryResourceInfoVO queryResourceInfo(Long resId) {
        ResourceInfo resourceInfo = mapper.queryResourceInfo(resId);
        if (resourceInfo != null) {
            QueryResourceInfoVO queryResourceInfoVO = new QueryResourceInfoVO();
            BeanUtils.copyProperties(resourceInfo,queryResourceInfoVO);
            queryResourceInfoVO.setCreateTime(DateUtil.formatDate(resourceInfo.getCreateTime(), DateUtil.FullFormatEnum.YMDHMS1));
            queryResourceInfoVO.setResStatusEnum(StatusEnum.getInstanceByCode(resourceInfo.getResStatus().intValue()));
            return queryResourceInfoVO;
        }
        return null;
    }
}
